<?php

class Buyback_View_Helper_Cart extends Zend_View_Helper_Abstract{

	public $cartModel;

    public function Cart()
    {
        $this->cartModel = new Buyback_Model_Cart();

        return $this;
    }

	public function getOfferPrice(){
    	return $this->formatAmount($this->cartModel->getOfferPrice()/100);
    }
    
    public function hasItem(){
    	return $this->cartModel->hasItem();
    }

    public function displayCart()
    {
    	$itemCount = $this->cartModel->getValidItemCount();
    	if(0 == $itemCount){
//    		if there are no valid items in the cart.
    		return "There are no items in the cart";
    	}
    	$i = 0;
    	$html = "";
    	$authors = "";
    	
    	foreach($this->cartModel->getSessionNamespace()->_items as $isbn => $item){

    		
    		$html .= "<div class='cart-item ";
    		$html .= ($i%2)?"odd":"even";
    		$html .= "'>";
    		$update = new Buyback_Form_Update(array(
    			'action'	=>	'/cart/update',
    			'method'	=>	'post'
    		));
    		$remove = new Buyback_Form_Remove(array(
    			'action'	=>	'/cart/remove',
    			'method'	=>	'post'
    		));
    		//Book source
    		if(Zend_Auth::getInstance()->getIdentity()):
    			if("admin" == Zend_Auth::getInstance()->getIdentity()->role):
					$html .= "<div class='item-source'>" . $item->ItemTypeObject->Source . "</div>";					
				endif;
			endif;
			
			//Book Image
			if($item->ItemTypeObject->Image){
				$imgsrc = $item->ItemTypeObject->Image;
			}else{
				$imgsrc = "/images/no-image.png";
			}
			$html .= "<div class='item-image'><img src=" . $imgsrc . "></img></div>";
			
			//Book Data
			$html .= "<div class='item-data'>";
				$html .= "<div class='item-isbn13'>Isbn13 : " . $item->ItemTypeObject->Isbn13 . "</div>";
				$html .= "<div class='item-isbn10'>Isbn10 : " . $item->ItemTypeObject->Isbn10 . "</div>";
				$html .= "<div class='item-title'>Title : " . $item->ItemTypeObject->Title . "</div>";
				if(is_array($item->ItemTypeObject->Authors)){
					foreach($item->ItemTypeObject->Authors as $author){
						if(0 == strlen($authors))
							$authors = $author;
						$authors .= ", " . $author;							
					}
					if(strlen($authors) > 50) 
					{
						$authors = substr($authors, 0, 47);
						$authors .= "...";
					}
					$html .= "<div class='item-authors'>Authors : " . $authors . "</div>";
				}else{
					$html .= "<div class='item-authors'>Author : " . $item->ItemTypeObject->Authors . "</div>";
				}
			$html .= "</div>";
			
			//Sub Form to remove Items
			$rem = new Zend_Form_SubForm();
			$rem->removeDecorator('DtDdWrapper');
			$rem->removeDecorator('Fieldset');
			$rem->removeDecorator('HtmlTag');
			$rem->addElement('hidden', 'isbn', array(
				'decorators'=>	$update->hiddenDecorators,
				'value'	=>	$isbn,
			));
			
			$remove->addSubForm($rem, 'rem');
			
			//Sub Form to update item quantity
			$qnty = new Zend_Form_SubForm();
			$qnty->removeDecorator('DtDdWrapper');
			$qnty->removeDecorator('Fieldset');
			$qnty->removeDecorator('HtmlTag');
			$qnty->addElement('hidden', 'maxqnty', array(
        		'decorators'=>	$update->splHiddenDecorators,
				'value'	=>	$item->MaxQnty,
				'label'	=>	'Max Quantity : ' .$item->MaxQnty
			));
        	$qnty->addElement('hidden', 'isbn', array(
        		'decorators'=>	$update->hiddenDecorators,
				'value'	=>	$isbn,
			));
			$qnty->addElement('text', 'qnty', array(
				'decorators'=>	$update->elementDecorators,
				'value'	=>	$item->Quantity,
				'label'	=>	'Quantity'
			));
			$qnty->addElement('image', 'updateItem', array(
				'decorators'=>	$update->buttonDecorators,
            	'label' => 'Update'
        	));
        	
			$update->addSubForm($qnty, 'qnty');
			

			//Book Price
			$html .= "<div class='item-price'>Offer : ";
				$html .= $this->formatAmount($item->Offers->OfferPrice/100);
			$html .= "</div>";
		
			//Book Quantity
			$html .= "<div class='item-qnty'>";
				$html .= $update;
				$html .= $remove; 
			$html .= "</div>";
			
				
			$html .= "</div>";
			
			$i++;
    	}
    	//Cart Offer
    	$html .= "<div class='cart-offer ";
    	$html .= ($i%2)?"odd":"even";
    	$html .= "'> Total Offer : " . $this->getOfferPrice() . "</div>";
    	
    	$remove->removeSubform('rem');
    	$remove->getElement('removeItem')->setLabel('Empty Cart');
    	$html .= "<div class='item-remove'>";
    		$html .= $remove;
    	$html .= "</div>";
    	$checkout = new Buyback_Form_Checkout(array(
    		'action'	=>	'/checkout',
    		'method'	=>	'post'
    	));
    	$checkout->getElement("checkout")->setAttrib("disabled", true);
    	if(($this->cartModel->getOfferPrice()/100)>=10){
    		$checkout->getElement("checkout")->setAttrib("disabled", null);
    	}
    	$html .= "<div class='item-checkout'>";
    		$html .= $checkout;
    	$html .= "</div>";
    	$html .= "<div class='clear'></div>";
    	return $html;
    }

	public function displayItems($isbnList)
    {
    	if(0 == count($isbnList)){
//    		if nothing was added to the cart
    		return;
    	}
    	
    	$i = 0;
    	$html = "";
    	$authors = "";
    	
    	foreach($isbnList as $key => $isbn){
				
    		if($this->cartModel->offsetExists($isbn)){
    		
    			$item = $this->cartModel->offsetGet($isbn);
	    		$html .= "<div class='cart-item ";
	    		$html .= ($i%2)?"odd":"even";
	    		$html .= "'>";
	    		$update = new Buyback_Form_Update(array(
	    			'action'	=>	'/cart/update',
	    			'method'	=>	'post'
	    		));
	    		//Book source
	    		if(Zend_Auth::getInstance()->getIdentity()):
					if("admin" == Zend_Auth::getInstance()->getIdentity()->role):
						$html .= "<div class='item-source'>" . $item->ItemTypeObject->Source . "</div>";					
					endif;
				endif;
				
				//Book Image
				if($item->ItemTypeObject->Image){
					$imgsrc = $item->ItemTypeObject->Image;
				}else{
					$imgsrc = "/images/no-image.png";
				}
				$html .= "<div class='item-image'><img src=" . $imgsrc . "></img></div>";
				
				//Book Data
				$html .= "<div class='item-data'>";
					$html .= "<div class='item-isbn13'>Isbn13 : " . $item->ItemTypeObject->Isbn13 . "</div>";
					$html .= "<div class='item-isbn10'>Isbn10 : " . $item->ItemTypeObject->Isbn10 . "</div>";
					$html .= "<div class='item-title'>Title : " . $item->ItemTypeObject->Title . "</div>";
					if(is_array($item->ItemTypeObject->Authors)){
						foreach($item->ItemTypeObject->Authors as $author){
							if(0 == strlen($authors))
								$authors = $author;
							$authors .= ", " . $author;						
						}
						if(strlen($authors) > 50){
							$authors = substr($authors, 0, 47);
							$authors .= "...";
						} 
						$html .= "<div class='item-authors'>Authors : " . $authors . "</div>";
					}else{
						$html .= "<div class='item-authors'>Author : " . $item->ItemTypeObject->Authors . "</div>";
					}
				$html .= "</div>";
				
				
				//Book Price
				$html .= "<div class='item-price'>Offer :";
					$html .= $this->formatAmount($item->Offers->OfferPrice/100);
				$html .= "</div>";
				
				//Book Quantity
				$html .= "<div class='item-qnty'>Quantity : ";
					$html .= $item->Quantity; 
				$html .= "</div>";
				
				
				$html .= "</div>";
			
			$i++;
    		}
    	}
    	
    	$html .= "<div class='clear'></div>";
    	return $html;
    }
    public function formatAmount($amount)
    {
        $currency = new Zend_Currency();
        return $currency->toCurrency($amount);
    }
}